<?
require_once(dirname(__FILE__).'/../models/Users.php');

class AdminController extends Main_Controller_Action {	
	public function preDispatch()
    {
        if (!unserialize(Zend_Auth::getInstance()->getIdentity())->getIsAdministrator() &&  !unserialize(Zend_Auth::getInstance()->getIdentity())->getIsOperator()) {        
                $this->_helper->redirector('', '');
        } else {
           /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
	
	function indexAction(){
		
		
		
		/**
		 * Authorization control - check if is admin or if is operator
		 * if is not looged -> access denied
		 */
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			if ($this->view->loggedUser->getIsAdministrator() == 1){
				$this->view->assign('isAdmin', $var = true);
			} else {
				$this->view->assign('isAdmin', $var = false);
			}
			if ($this->view->loggedUser->getIsOperator() == 1){
				$this->view->assign('isOperator', $var = true);
			} else {
				$this->view->assign('isOperator', $var = false);
			}
			
			
		} else {
			echo $tLocale->t("Access denied")."!!!";
			//exit();
		}
		
	}
	
	function cleanSqlAction(){
		
	}
	
	function doCleanSqlAction(){
		
		$dirtySQL = $this->_request->getParam('dirtySQL');
		$dirtySQL = str_replace("CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER", "ALTER", $dirtySQL);
		$dirtySQL = str_ireplace("AS select", "AS\nSELECT", $dirtySQL);
		$dirtySQL = str_ireplace("`", "", $dirtySQL);
		$dirtySQL = str_ireplace(",", ",\n\t", $dirtySQL);
		$dirtySQL = str_ireplace("on(", "ON ", $dirtySQL);
		$dirtySQL = str_ireplace("(", "", $dirtySQL);
		$dirtySQL = str_ireplace(")", "", $dirtySQL);
		$dirtySQL = str_ireplace("left join", "\nLEFT JOIN", $dirtySQL);
		$dirtySQL = str_ireplace("from", "\nFROM", $dirtySQL);
		
		echo "<pre>".$dirtySQL."</pre>";
		exit();
	}
}

?>